var CreateGame = require('./modules/createGame/index.js');
var CreatePrize = require('./modules/createPrize/index.js');
var PrizeList = require('./modules/prizeList/index.js');
var Data = require('/src/modules/data.js');

var limit = {
    type0:6,
    type1:12,
    type4:0
};

new Vue({
    el: '#wrapper',
    components:{
        creategame:CreateGame,
        createprize:CreatePrize,
        prizelist:PrizeList
    },
    data:{
        lottery:{
            type:'0',
            title:'',
            perDayLimit:'',
            remark:'',
            bannerUrl:'',
            sceneCode:[]
        },
        prizes:[],
        limit:limit
    },
    beforeCreate:function(){
        var id = getQueryString('id');
        if(!id){
            return;
        }
        Data.getLottery({id:id},function(res){
            res.data.lotteryDO.sceneCode = JSON.parse(res.data.lotteryDO.sceneCode);
            this.$data.lottery=res.data.lotteryDO;
            this.$data.prizes = res.data.lotteryRewardDOs;
        }.bind(this))
    },
    methods:{
        nextStep:function(){
            var lottery = this.lottery;
            var keys = Object.keys(lottery);
            var checkNull=false;
            keys.forEach(function(key){
                if(!lottery[key] && lottery[key]!==0){
                    checkNull=true;
                }
            });

            if(checkNull || !lottery.sceneCode.length){
                alert('请填写完整');
                return;
            }

            var limit  = this.limit['type'+this.lottery.type];
            if(this.prizes.length<limit){
                alert('需要配置'+limit+'个奖品');
                return;
            }

            var result = {
                lottery:this.lottery,
                prizes:this.prizes
            };
            sessionStorage.setItem('gameconfig',JSON.stringify(result));
            window.location.href='../configGame/index.html';
        },
        listenmsgfromchild:function(src){
            this.lottery.bannerUrl = src;
        },
        listenprizes:function(data){
            console.log(data);
        }
    }
});
